// packageD/quickPay/member_add_bank_second.js
let app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    bank: [],
    payer_name: "",
    payer_phone: "",
    member_phone: "",
    v2_code: "",
    id_no: "",
    cvv: "",
    date: "2016-06",
    card_no: "",
    bank_name: "",
    bank_type: "",
    id_type: "",
    user_agrt: "",
    checked: true,
    show2: false,
    show3: false,
    show4: false,
    show5: false,
    radio: "",
    radio1: "",
    radio2: "",
    idType: [
      {
        id: "1",
        text: "身份证",
      },
      {
        id: "2",
        text: "军官证",
      },
      {
        id: "3",
        text: "士兵证",
      },
      {
        id: "4",
        text: "护照",
      },
      {
        id: "5",
        text: "港澳台居民往来通行证",
      },
      {
        id: "6",
        text: "临时身份证",
      },
      {
        id: "7",
        text: "户口本",
      },
      {
        id: "8",
        text: "警官证",
      },
      {
        id: "9",
        text: "外国人永久居留证",
      },
      {
        id: "10",
        text: "其他",
      },
      {
        id: "11",
        text: "外国护照",
      },
      {
        id: "12",
        text: "营业执照",
      },
    ],
    card: [
      {
        id: "0",
        text: "储蓄卡",
      },
      {
        id: "1",
        text: "信用卡",
      },
    ],
    datas: [
      {
        id: "1",
        text: "中国工商银行",
      },
      {
        id: "2",
        text: "中国银行",
      },
      {
        id: "3",
        text: "中信银行",
      },
      {
        id: "4",
        text: "上海银行",
      },
      {
        id: "5",
        text: "中国建设银行",
      },
      {
        id: "6",
        text: "光大银行",
      },
      {
        id: "7",
        text: "民生银行",
      },
      {
        id: "8",
        text: "北京银行",
      },
      {
        id: "9",
        text: "平安银行",
      },
      {
        id: "10",
        text: "交通银行",
      },
      {
        id: "11",
        text: "招商银行",
      },
      {
        id: "12",
        text: "广发银行",
      },
      {
        id: "13",
        text: "浦发银行",
      },
      {
        id: "14",
        text: "邮政银行",
      },
    ],
    count: '',
    check_open: false,
    isOpenCaptcha: false,
    captcha: '',
    imgCodeLink: '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.initData();
    let option = options;
    this.setData({
      card_no: option.card_no,
    });
    this.getMemberPhone();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    this.data.timer && clearInterval(this.data.timer);
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {},

  // 获取会员绑定的手机号
  getMemberPhone () {
    let that = this;
    let urlStr = app.getNetAddresss("plugin.converge_pay.frontend.controllers.quick-pay-bank-card.get-mobile");
    app._getNetWork({
      url: urlStr,
      showToastIn: false,
      success: (resdata) => {
        var res = resdata.data;
        if (res.result !== 1) {
          wx.showToast({
            icon: "none",
            title: res.msg,
            duration: 1500,
          });
          return;
        }
        this.setData({
          check_open: res.data.sms_check_open
        });
        if (res.data.get_captcha && res.data.get_captcha.captcha) {
          this.setData({
            imgCodeLink: res.data.get_captcha.captcha.img,
            isOpenCaptcha: res.data.get_captcha.captcha.status
          });
        }
        if (res.data.mobile) {
          that.setData({
            member_phone: res.data.mobile
          });
          that.getAgrement();
          that.getInfo();
          that.getBank();
        } else {
          wx.showModal({
            title: '提示',
            content: '商城会员没有绑定手机号，请绑定手机号',
            success (result) {
              if (result.confirm) {
                wx.navigateTo({
                  url: '/packageA/member/editmobile/editmobile',
                });
              } else if (result.cancel) {
                wx.navigateBack({
                  delta: 1,
                });
              }
            }
          });
        }
      },
      fail: function (res) {
        console.log(res.msg);
      },
    });
  },
  nameInp(e) {
    let val = e.detail.value;
    this.setData({
      payer_name: val,
    });
  },
  phoneInp(e) {
    let val = e.detail.value;
    this.setData({
      payer_phone: val,
    });
  },
  memberPhoneInp (e) {
    let val = e.detail.value;
    this.setData({
      member_phone: val,
    });
  },
  changeCaptcha (e) {
    let val = e.detail.value;
    this.setData({
      captcha: val
    });
  },
  v2CodeInp (e) {
    let val = e.detail.value;
    this.setData({
      v2_code: val,
    });
  },
  idInp(e) {
    let val = e.detail.value;
    this.setData({
      id_no: val,
    });
  },
  cvvInp(e) {
    let val = e.detail.value;
    this.setData({
      cvv: val,
    });
  },
  bindDateChange: function (e) {
    this.setData({
      date: e.detail.value,
    });
  },
  openBank() {
    this.setData({
      show2: true,
    });
  },
  closeBank() {
    this.setData({
      show2: false,
    });
  },
  openCard() {
    this.setData({
      show3: true,
    });
  },
  closeCard() {
    this.setData({
      show3: false,
    });
  },
  openType() {
    this.setData({
      show4: true,
    });
  },
  closeType() {
    this.setData({
      show4: false,
    });
  },
  bankChange(e) {
    let val = e.detail;
    this.setData({
      radio: val,
    });
  },
  cardChange(e) {
    let val = e.detail;
    this.setData({
      radio1: val,
    });
  },
  idChange(e) {
    let val = e.detail;
    this.setData({
      radio2: val,
    });
  },
  openAgg() {
    this.setData({
      show5: true,
    });
  },
  closeAgg() {
    this.setData({
      show5: false,
    });
  },
  confirm() {
    this.setData({
      show2: false,
    });
    this.data.datas.forEach((item, index, key) => {
      if (item.id == this.data.radio) {
        this.setData({
          bank_name: item.text,
        });
      }
    });
  },
  cardConfirm() {
    this.setData({
      show3: false,
    });
    this.data.card.forEach((item, index, key) => {
      if (item.id == this.data.radio1) {
        this.setData({
          bank_type: item.text,
        });
      }
    });
  },
  typeConfirm() {
    this.setData({
      show4: false,
    });
    this.data.idType.forEach((item, index, key) => {
      if (item.id == this.data.radio2) {
        this.setData({
          id_type: item.text,
        });
      }
    });
  },
  toSelect(e) {
    this.setData({
      checked: e.detail,
    });
  },
  initData() {
    this.setData({
      bank: [],
      payer_name: "",
      payer_phone: "",
      id_no: "",
      cvv: "",
      date: "2016-06",
      card_no: "",
      bank_name: "",
      bank_type: "",
      id_type: "",
      user_agrt: "",
      checked: true,
      show2: false,
      show3: false,
      show4: false,
      show5: false,
      radio: "",
      radio1: "",
      radio2: "",
      idType: [
        {
          id: "1",
          text: "身份证",
        },
        {
          id: "2",
          text: "军官证",
        },
        {
          id: "3",
          text: "士兵证",
        },
        {
          id: "4",
          text: "护照",
        },
        {
          id: "5",
          text: "港澳台居民往来通行证",
        },
        {
          id: "6",
          text: "临时身份证",
        },
        {
          id: "7",
          text: "户口本",
        },
        {
          id: "8",
          text: "警官证",
        },
        {
          id: "9",
          text: "外国人永久居留证",
        },
        {
          id: "10",
          text: "其他",
        },
        {
          id: "11",
          text: "外国护照",
        },
        {
          id: "12",
          text: "营业执照",
        },
      ],
      card: [
        {
          id: "0",
          text: "储蓄卡",
        },
        {
          id: "1",
          text: "信用卡",
        },
      ],
      datas: [
        {
          id: "1",
          text: "中国工商银行",
        },
        {
          id: "2",
          text: "中国银行",
        },
        {
          id: "3",
          text: "中信银行",
        },
        {
          id: "4",
          text: "上海银行",
        },
        {
          id: "5",
          text: "中国建设银行",
        },
        {
          id: "6",
          text: "光大银行",
        },
        {
          id: "7",
          text: "民生银行",
        },
        {
          id: "8",
          text: "北京银行",
        },
        {
          id: "9",
          text: "平安银行",
        },
        {
          id: "10",
          text: "交通银行",
        },
        {
          id: "11",
          text: "招商银行",
        },
        {
          id: "12",
          text: "广发银行",
        },
        {
          id: "13",
          text: "浦发银行",
        },
        {
          id: "14",
          text: "邮政银行",
        },
      ],
      check_open: false,
      isOpenCaptcha: false,
      captcha: '',
      imgCodeLink: '',
    });
  },
  getBank() {
    let that = this;
    let urlStr = app.getNetAddresss(
      "plugin.converge_pay.frontend.controllers.quick-pay-bank-card.index"
    );
    app._getNetWork({
      url: urlStr,
      showToastIn: false,
      success: function (resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          that.setData({
            bank: res.data,
          });
        } else {
          that.setData({
            bank: res.data,
          });
        }
      },
      fail: function (res) {
        console.log(res.msg);
      },
    });
  },
  getInfo() {
    let that = this;
    let urlStr = app.getNetAddresss(
      "plugin.converge_pay.frontend.controllers.quick-pay-bank-card.get-payer-info"
    );
    app._getNetWork({
      url: urlStr,
      showToastIn: false,
      success: function (resdata) {
        var res = resdata.data;
        if (res.result == 1 && res.data) {
          that.setData({
            payer_name: res.data.payer_name,
            radio2: res.data.id_type,
            id_no: res.data.id_no,
          });
        }
      },
      fail: function (res) {
        console.log(res.msg);
      },
    });
  },
  shijian() {
    let y = this.data.date.split("-")[0];
    let m = this.data.date.split("-")[1];
    return `${y}/${m}`;
  },
  getType(id) {
    let urlStr = app.getNetAddresss(
      "plugin.converge_pay.frontend.controllers.quick-pay-bank-card.get-pay-type"
    );
    app._getNetWork({
      url: urlStr,
      showToastIn: false,
      success: function (resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          if (res.data.pay_type == 1) {
            wx.navigateTo({
              url: "/packageD/quickPay/member_choose_bank/member_choose_bank",
            });
          } else {
            wx.navigateTo({
              url:
                "/packageD/quickPay/member_bind_bank/member_bind_bank?card_id=" +
                id,
            });
          }
        } else {
          wx.showToast({
            icon: "none",
            title: res.msg,
            duration: 1500,
          });
        }
      },
      fail: function (res) {
        console.log(res.msg);
      },
    });
  },
  getAgrement() {
    let that = this;
    let urlStr = app.getNetAddresss(
      "plugin.converge_pay.frontend.controllers.quick-pay-bank-card.get-user-agrt"
    );
    app._getNetWork({
      url: urlStr,
      showToastIn: false,
      success: function (resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          that.setData({
            user_agrt: res.data.user_agrt,
          });
        } else {
          wx.showToast({
            icon: "none",
            title: res.msg,
            duration: 1500,
          });
        }
      },
      fail: function (res) {
        console.log(res.msg);
      },
    });
  },

  Toast (text) {
    wx.showToast({
      title: text,
      icon: "none",
      duration: 1500
    });
  },

  addBank() {
    if (app._isTextEmpty(this.data.bank_name)) {
      this.Toast("请选择所属银行");
      return;
    }
    if (app._isTextEmpty(this.data.radio1)) {
      this.Toast("请选择银行卡类型");
      return;
    }
    if (app._isTextEmpty(this.data.date) && this.data.radio1 == "1") {
      this.Toast("请选择信用卡有效期");
      return;
    }
    if (app._isTextEmpty(this.data.cvv) && this.data.radio1 == "1") {
      this.Toast("请填写安全码");
      return;
    }
    if (app._isTextEmpty(this.data.payer_name)) {
      this.Toast("请输入持卡人姓名");
      return;
    }
    if (app._isTextEmpty(this.data.payer_phone)) {
      this.Toast("请输入银行预留手机号");
      return;
    }
    if (this.data.check_open == 1 && app._isTextEmpty(this.data.member_phone)) {
      this.Toast("请输入绑定手机号");
      return;
    }
    if (this.data.check_open == 1 && app._isTextEmpty(this.data.v2_code)) {
      this.Toast("请输入验证码");
      return;
    }
    if (app._isTextEmpty(this.data.checked)) {
      this.Toast("请同意用户协议");
      return;
    }
    var json;
    if (this.data.radio1 == "0") {
      json = {
        data: {
          card_no: this.data.card_no,
          payer_name: this.data.payer_name,
          payer_phone: this.data.payer_phone,
          id_no: this.data.id_no,
          id_type: this.data.radio2,
          bank_type: this.data.radio1,
          bank_name: this.data.bank_name,
        }
      };
    } else if (this.data.radio1 == "1") {
      json = {
        data: {
          card_no: this.data.card_no,
          payer_name: this.data.payer_name,
          payer_phone: this.data.payer_phone,
          id_no: this.data.id_no,
          id_type: this.data.radio2,
          bank_type: this.data.radio1,
          bank_name: this.data.bank_name,
          expire_date: this.shijian(this.data.date),
          cvv: this.data.cvv,
        }
      };
    }
    if (this.data.check_open == 1) {
      json.code = this.data.v2_code;
    }

    let that = this;
    let urlStr = app.getNetAddresss(
      "plugin.converge_pay.frontend.controllers.quick-pay-bank-card.create"
    );
    app._postNetWork({
      url: urlStr,
      showToastIn: false,
      data: json,
      success: function (resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          app.tips("添加成功");
          that.getType(res.data.id);
        } else {
          app.tips(res.msg);
        }
      },
      fail: function (res) {
        console.log(res.msg);
      },
    });
  },

  // 刷新图形验证码
  getCaptchaImg () {
    let urlStr = app.getNetAddresss("plugin.converge_pay.frontend.controllers.quick-pay-bank-card.get-mobile");
    app._getNetWork({
      url: urlStr,
      data: {},
      success: (response) => {
        let res = response.data;
        if (res.result !== 1) {
          app.tips(res.msg);
        }
        if (res.data.get_captcha && res.data.get_captcha.captcha) {
          this.setData({
            imgCodeLink: res.data.get_captcha.captcha.img
          });
        }
      }
    });
  },

  // 获取验证码
  VerificationCode() {
    if (this.data.isOpenCaptcha == 1 && app._isTextEmpty(this.data.captcha)) {
      app.tips("请填写图形验证码");
      return;
    }
    if (app._isTextEmpty(this.data.member_phone)) {
      app.tips("请填写需要绑定的手机号");
      return;
    }
    wx.showLoading({
      title: "发送中",
    });
    var that = this;
    var urlStr = app.getNetAddresss("plugin.converge_pay.frontend.controllers.quick-pay-bank-card.send-code");
    app._getNetWork({
      showToastIn: false,
      url: urlStr,
      data: { captcha: this.data.captcha },
      success: function (resdata) {
        var res = resdata.data;
        wx.hideLoading();
        if (res.result == 1) {
          that.getCode();
        } else {
          app.tips(res.msg);
        }
      },
      fail: function (res) {},
    });
  },
  // 60秒倒计时
  getCode() {
    const TIME_COUNT = 60;
    if (!this.data.timer) {
      this.data.count = TIME_COUNT;
      this.data.timer = setInterval(() => {
        if (this.data.count > 0 && this.data.count <= TIME_COUNT) {
          this.data.count--;
          this.setData({
            count:this.data.count
          });
        } else {
          clearInterval(this.timer);
          this.data.count = 0;
          this.data.timer = null;
          this.setData({
            count:this.data.count,
            timer:this.data.timer
          });
        }
      }, 1000);
    }
  },

});
